/* Copyright (c) [2025] [zhi1208@126.com]
 * SPDX-License-Identifier: MIT
 */

/*********************
 *      INCLUDES
 *********************/
#include "lvgl.h"
#include "hmi_subject.h"
#include "hmi_theme.h"
#include "hmi_translation.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/
#define CITY_NAME_LEN_MAX 32
#define WEATHER_TEXT_LEN_MAX 32

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void theme_subj_observer_cb(lv_observer_t *observer, lv_subject_t *subject);
static void translations_subj_observer_cb(lv_observer_t *observer, lv_subject_t *subject);
static void rotate_subj_observer_cb(lv_observer_t *observer, lv_subject_t *subject);

/**********************
 *  STATIC VARIABLES
 **********************/
static lv_subject_t year_subj;
static lv_subject_t month_subj;
static lv_subject_t day_subj;
static lv_subject_t weekday_subj;
static lv_subject_t hour_subj;
static lv_subject_t minute_subj;
static lv_subject_t *time_subj_list[] = {&year_subj, &month_subj, &day_subj, &weekday_subj, &hour_subj, &minute_subj};

static lv_subject_t city_subj;
static lv_subject_t text_day_subj;
static lv_subject_t code_day_subj;
static lv_subject_t text_night_subj;
static lv_subject_t code_night_subj;
static lv_subject_t high_subj;
static lv_subject_t low_subj;
static lv_subject_t *weather_subj[] = {&city_subj, &text_day_subj, &code_day_subj, &text_night_subj, &code_night_subj, &high_subj, &low_subj};

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
float hmi_g_anim_rate = 1;

lv_subject_t hmi_time_subj;
lv_subject_t hmi_wifi_subj;
lv_subject_t hmi_bluetooth_subj;

lv_subject_t hmi_theme_subj;
lv_subject_t hmi_translations_subj;
lv_subject_t hmi_rotate_subj;

lv_subject_t hmi_weather_subj;

void hmi_subject_init()
{
    hmi_theme_init();
    hmi_translation_init();

    lv_subject_init_int(&hmi_theme_subj, HMI_THEME_LIGHT);
    lv_subject_init_int(&hmi_translations_subj, HMI_TRANS_ZH);
    lv_subject_init_int(&hmi_rotate_subj, HMI_ROTAE_DEF);

    lv_subject_add_observer(&hmi_theme_subj, theme_subj_observer_cb, NULL);
    lv_subject_add_observer(&hmi_translations_subj, translations_subj_observer_cb, NULL);
    lv_subject_add_observer(&hmi_rotate_subj, rotate_subj_observer_cb, NULL);

    lv_subject_init_int(&year_subj, 2025);
    lv_subject_init_int(&month_subj, 7);
    lv_subject_init_int(&day_subj, 27);
    lv_subject_init_int(&weekday_subj, 1);
    lv_subject_init_int(&hour_subj, 0);
    lv_subject_init_int(&minute_subj, 0);
    lv_subject_init_group(&hmi_time_subj, time_subj_list, sizeof(time_subj_list) / sizeof(lv_subject_t *));

    lv_subject_init_int(&hmi_wifi_subj, HMI_STATE_OFF);
    lv_subject_init_int(&hmi_bluetooth_subj, HMI_STATE_OFF);

    static char city[CITY_NAME_LEN_MAX];
    lv_subject_init_string(&city_subj, city, NULL, CITY_NAME_LEN_MAX, lv_tr("北京"));
    static char text_day[WEATHER_TEXT_LEN_MAX];
    lv_subject_init_string(&text_day_subj, text_day, NULL, WEATHER_TEXT_LEN_MAX, lv_tr("晴"));
    lv_subject_init_int(&code_day_subj, 0);
    static char text_night[WEATHER_TEXT_LEN_MAX];
    lv_subject_init_string(&text_night_subj, text_night, NULL, WEATHER_TEXT_LEN_MAX, lv_tr("晴"));
    lv_subject_init_int(&code_night_subj, 0);
    lv_subject_init_int(&high_subj, 22);
    lv_subject_init_int(&low_subj, 28);
    lv_subject_init_group(&hmi_weather_subj, weather_subj, sizeof(weather_subj) / sizeof(lv_subject_t *));
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void theme_subj_observer_cb(lv_observer_t *observer, lv_subject_t *subject)
{
    LV_UNUSED(observer);

    int32_t theme = lv_subject_get_int(subject);

    switch (theme)
    {
    case HMI_THEME_DARK:
        hmi_color_master = lv_color_hex(HMI_COLOR_DARK);

        lv_style_set_text_color(&hmi_g_wrap_style, lv_color_white());

        lv_style_set_image_recolor(&hmi_g_icon_style, lv_color_white());
        break;

    case HMI_THEME_LIGHT:
        hmi_color_master = lv_color_hex(HMI_COLOR_LIGHT);

        lv_style_set_text_color(&hmi_g_wrap_style, lv_color_black());

        lv_style_set_image_recolor(&hmi_g_icon_style, lv_color_black());
        break;

    default:
        break;
    }

    lv_style_set_bg_color(&hmi_g_wrap_style, hmi_color_master);
    lv_obj_report_style_change(NULL);
}

static void translations_subj_observer_cb(lv_observer_t *observer, lv_subject_t *subject)
{
    LV_UNUSED(observer);

    int32_t lang = lv_subject_get_int(subject);

    switch (lang)
    {
    case HMI_TRANS_ZH:
        lv_translation_set_language("zh");
        break;

    case HMI_TRANS_EN:
        lv_translation_set_language("en");
        break;

    default:
        break;
    }
}

static void rotate_subj_observer_cb(lv_observer_t *observer, lv_subject_t *subject)
{
    LV_UNUSED(observer);

    int32_t rotation = lv_subject_get_int(subject);

    switch (rotation)
    {
    case HMI_ROTAE_DEF:
        break;

    case HMI_ROTAE_90:
        break;

    default:
        break;
    }
}
